/* 
 *  <copyright file="ProcessGameService.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;

using SobrietyEngine.Common;
using SobrietyEngine.Guids;

namespace SobrietyEngine.Processes
{
    public class ProcessGameService:GameComponent
    {
        List<IProcess> ProcessList;
        Double CurrentTime;

        public ProcessGameService(Microsoft.Xna.Framework.Game game):base(game)
        {
            ProcessList = new List<IProcess>();
            CurrentTime = 0;
        }

        public override void Update(GameTime gameTime)
        {
            CurrentTime += gameTime.ElapsedGameTime.Milliseconds;
            while (ProcessList.Count > 0 && ProcessList[0].GetTime() <= CurrentTime)
            {
                ProcessList[0].DoProcess();
                ProcessList[0].Kill();
                ProcessList.RemoveAt(0);
            }

            if (ProcessList.Count == 0)
                CurrentTime = 0;           
        }

        public IProcess Create(string type, Parameters parms)
        {
            return Create(type, parms, (string)parms["guid"] );
        }

        public IProcess Create(string type, Parameters parms, string guid)
        {
            IProcess proc = ProcessFactory.Build(type, parms);
            CGuid cGuid = CGuid.Create(guid, proc);
            proc.SetGuid(cGuid);
            return proc;
        }

        public void Add(IProcess proc)
        {
            if (proc == null)
                return;

            proc.SetTime(proc.GetTime() + CurrentTime);

            for (int i = ProcessList.Count - 1; i >= 0; --i)
            {
                if (ProcessList[i].GetTime() <= proc.GetTime())
                {
                    ProcessList.Insert(i, proc);
                }
            }
        }

        public void Remove(CGuid guid)
        {
            for (int i = 0; i < ProcessList.Count; ++i)
            {
                if (ProcessList[i].GetGuid().Value == guid.Value)
                {
                    ProcessList[i].Kill();
                    ProcessList.RemoveAt(i);                    
                    return;
                }
            }
        }

        public void Clear()
        {
            while (ProcessList.Count > 0)
            {
                ProcessList[0].Kill();
                ProcessList.RemoveAt(0);
            }
        }
    }
}